package com.rjwh.dingdong.client.adapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.BaseAdapter;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.rjwh.dingdong.client.R;
import com.rjwh.dingdong.client.util.PicPathUtil;

import java.io.File;
import java.util.List;

/**
 * 图片选择列表
 *
 * @author 王小珂
 * @ClassName PicAdapter
 * @Description TODO(这里用一句话描述这个类的作用)
 * @date 2015-10-20 上午10:04:33
 * @history 1.YYYY-MM-DD author: description:
 */
public class MyPicAdapter extends BaseAdapter {

    private Context context;
    private List<String> attachList;
    private List<String> localList;
    private BitmapFactory.Options options;
    private int screen;

    public MyPicAdapter(Context c, List<String> attachList, List<String> localList, int screens) {
        context = c;
        this.attachList = attachList;
        this.localList = localList;
        options = new BitmapFactory.Options();
        options.inSampleSize = 3;
        screen = screens;
    }

    @Override
    public int getCount() {

        if (attachList != null && !attachList.isEmpty()) {
            if (attachList.size() > 3) {// 设置最多显示3张图片
                return 3;
            } else {
                return attachList.size();
            }
        } else {
            return 0;
        }
    }

    @Override
    public Object getItem(int arg0) {
        if (localList != null && !localList.isEmpty()) {
            return localList.get(arg0);
        }
        return attachList.get(arg0);
    }

    @Override
    public long getItemId(int arg0) {
        return arg0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup arg2) {

        View view = convertView;
        final ViewHolder holder;

        if (convertView == null) {
            view = View.inflate(context, R.layout.grid_pic_item, null);
            holder = new ViewHolder();
            holder.ivPhoto = (ImageView) view.findViewById(R.id.pic_item_image);

            LayoutParams ivPhotoLp = holder.ivPhoto.getLayoutParams();

            ivPhotoLp.width = (int) screen / 2;
            ivPhotoLp.height = (int) screen / 2;

            holder.ivPhoto.setLayoutParams(ivPhotoLp);
            view.setTag(holder);
        } else {
            holder = (ViewHolder) view.getTag();
        }

        if (localList != null && !localList.isEmpty()) {
            final String imgLocalPath = localList.get(position);
            holder.ivPhoto.setImageBitmap(BitmapFactory.decodeFile(imgLocalPath, options));
            holder.ivPhoto.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    File file = new File(imgLocalPath);
                    if (file != null && file.isFile() == true) {
                        Intent intent = new Intent();
                        intent.setAction(Intent.ACTION_VIEW);
                        intent.setDataAndType(Uri.fromFile(file), "image/*");
                        context.startActivity(intent);
                    }
                }
            });
        } else {
            String imageUrl = attachList.get(position);
            Glide.with(context).load(PicPathUtil.getPicUrl(imageUrl)).placeholder(R.drawable.icon_pic_holder).error(R.drawable.icon_pic_holder).dontAnimate().diskCacheStrategy(DiskCacheStrategy.ALL).into(holder.ivPhoto);
        }
        return view;
    }

    private class ViewHolder {
        private ImageView ivPhoto;
    }

}
